package com.example.taximeter.controller;


import com.example.taximeter.bean.*;
import com.example.taximeter.service.RoleService;
import com.example.taximeter.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色
 */
@RestController
@RequestMapping(value = "/role")
public class RoleController {

    private RoleService roleService;
    private UserService userService;
//    private final static Logger logger = LoggerFactory.getLogger(ParamFileController.class);


    @Autowired
    public RoleController(RoleService roleService,UserService userService){
        this.roleService = roleService;
        this.userService = userService;
    }

    @RequestMapping(value = "/list")
    public List<Role> findRoleList(){
        return roleService.findRolelist();
    }

    //添加角色
    @RequestMapping(value = "/add")
    public ReturnInfo addRole(@RequestBody @Validated(GroupCreate.class) Role role){
        return ReturnInfo.info1(roleService.addRole(role));
    }

    //查看角色列表
    @RequestMapping(value = "/all")
    public ReturnInfo findRoleAll(@RequestParam(value = "pageNum", defaultValue = "0") String pageNum,
                                  @RequestParam(value = "pageSize", defaultValue = "0") String pageSize,
                                  @RequestBody Role role) {
        //添加日志
        userService.addUserLog(7,role.toString());
        return roleService.findRoleAll(Integer.valueOf(pageNum), Integer.valueOf(pageSize), role);
    }

    //修改角色名
    @RequestMapping(value = "/update")
    public ReturnInfo updateRole(@RequestBody Role role){
        String mes =roleService.updateRole(role);
        if(mes.equals("成功")){
//            logger.info("修改角色名"+role.toString());
            //添加日志
            userService.addUserLog(5,role.toString());
        }
        return ReturnInfo.info1(mes);
    }

    //删除角色
    @RequestMapping(value = "/delete/{roleid}")
    public ReturnInfo deleteRole(@PathVariable Integer roleid){
        String mes =roleService.deleteRole(roleid);
        if(mes.equals("成功")){
//            logger.info("删除角色"+String.valueOf(roleid));
            //添加日志
            userService.addUserLog(6,String.valueOf(roleid));
        }
        return ReturnInfo.info1(mes);
    }

    //修改角色权限
    @RequestMapping(value = "/reset")
    public ReturnInfo updateRolePriv(@RequestBody Role role){
        String mes =roleService.updateRolePriv(role);
        if(mes.equals("成功")){
//            logger.info("修改角色"+role.toString());
            //添加日志
            userService.addUserLog(5,role.toString());
        }
        return ReturnInfo.info1(mes);
    }


}
